package com.xuanyang.dormrepair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xuanyang.dormrepair.config.hook.AdminLogin;
import com.xuanyang.dormrepair.model.entity.FloorRoom;
import com.xuanyang.dormrepair.service.FloorRoomService;
import com.xuanyang.dormrepair.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2019/5/23 2:09
 * @DESCRIBE 宿舍管理
 */
@AdminLogin
@RestController
@RequestMapping("/admin/froom")
public class FloorRoomController {

    @Autowired
    private FloorRoomService roomService;

    /**
     * 获取所有的楼层
     */
    @PostMapping("/floors")
    public RestResponse floors() {
        List<String> floors = roomService.floors();
        if (!floors.isEmpty()) {
            return RestResponse.ok(floors);
        }
        return RestResponse.fail();
    }

    @PostMapping("/page")
    public RestResponse page(int page, int limit, FloorRoom query) {
        IPage<FloorRoom> iPage = roomService.page(page, limit, query);
        if (iPage.getTotal() > 0) {
            return RestResponse.ok(iPage);
        }
        return RestResponse.fail();
    }

    @PostMapping("/add")
    public RestResponse register(FloorRoom room) {
        if (StringUtils.isEmpty(room.getPassword())) {
            room.setPassword("123456");
        }
        if (roomService.register(room)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/del/{id}")
    public RestResponse del(@PathVariable Integer id) {
        if (roomService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/change")
    public RestResponse change(FloorRoom room) {
        if (roomService.update(room)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
